package com.zbf.weixinwangyeshouqaun;

import com.egzosn.pay.common.util.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class QrCodeUtil2 {

	/**设定二维码的宽度**/
	private static final int width = 110;
	/**设定二维码的高度**/
	private static final int height = 110;
	/**设定二维码的图片格式**/
	private static final String format = "png";

	private static String url;

	private static final int IMAGE_HALF_WIDTH = width / 2;
	private static final int FRAME_WIDTH = 2;


	/**
	 * @author: LCG
	 * @date: 2022/3/11
	 * @description:  生成二维码，获取二维码的字节数组  不带中心的logo图片
	 */
	public static byte[] createQRCodeByte(String url){
		QrCodeUtil2.url = url;
		//对二维码进行必要的设定
		Map<EncodeHintType, Object> hints = new HashMap<>();
		//设定内容的编码格式
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		//设定图像的错误校正程度：
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		//设定图像外边距（像素）
		hints.put(EncodeHintType.MARGIN, 0);
		try {
			//进行编码并获得一个bit封装对象
			BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
			BitMatrix bitMatrix1 = deleteWhite(bitMatrix);
			ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
			MatrixToImageWriter.writeToStream(bitMatrix1, "PNG", pngOutputStream);
			byte[] pngData = pngOutputStream.toByteArray();
			return  pngData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

    /** 
     * @author: LCG
     * @date: 2022/3/21
     * @description:  去掉白色边框
     */
	public static BitMatrix deleteWhite(BitMatrix matrix){
		int[] rec = matrix.getEnclosingRectangle();
		int resWidth = rec[2];
		int resHeight = rec[3];
		BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
		resMatrix.clear();
		for (int i = 0; i < resWidth; i++) {
			for (int j = 0; j < resHeight; j++) {
				if (matrix.get(i + rec[0], j + rec[1])){
					resMatrix.set(i, j);
				}

			}
		}
		return resMatrix;
	}


	public static BufferedImage getQrcodeImage(String url){
		QrCodeUtil2.url = url;
		//对二维码进行必要的设定
		Map<EncodeHintType, Object> hints = new HashMap<>();
		//设定内容的编码格式
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		//设定图像的错误校正程度：
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		//设定图像外边距（像素）
		hints.put(EncodeHintType.MARGIN, 0);
		try {
			//进行编码并获得一个bit封装对象
			BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
			BitMatrix bitMatrix1 = deleteWhite(bitMatrix);
			ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
			MatrixToImageWriter.writeToStream(bitMatrix1, "PNG", pngOutputStream);
			InputStream in = new ByteArrayInputStream(pngOutputStream.toByteArray());
			BufferedImage bImageFromConvert = ImageIO.read(in);
            return bImageFromConvert;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


    /** 
     * @author: LCG
     * @date: 2022/3/21
     * @description:  获取带logo图片的二维码
     */
	public static byte[] createQrcodeLogoImage(String url,String logoFile,int imageWidth,int imageHeight) throws IOException {
		BufferedImage bufImg = getQrcodeImage(url);
		Image im = ImageIO.read(new File(logoFile));
		Graphics2D g = bufImg.createGraphics();
		//获取bufImg的中间位置
		int centerX = bufImg.getMinX() + bufImg.getWidth()/2 - imageWidth/2;
		int centerY = bufImg.getMinY() + bufImg.getHeight()/2 - imageHeight/2;
		g.drawImage(im,centerX,centerY,imageWidth,imageHeight,null);
		g.dispose();
		bufImg.flush();

		ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
		ImageIO.write(bufImg,"png",byteArrayOutputStream);
		byte[] bytes = byteArrayOutputStream.toByteArray();
		byteArrayOutputStream.close();
		return bytes;
	}


}